package com.zph.dao;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;

import com.zph.entity.Constants;
import com.zph.entity.DataHead;
import com.zph.entity.DataStructure;

public class ParseList {
	public static HashMap<String, Integer> m_UserDic = new HashMap<String, Integer>(200, 0.75f);
	private static final DataFormatter FORMATTER = new DataFormatter();

	static {
		init(Constants.User1);
		// init("D:/zzh_file/User1.txt");
	}
	
	/**
	 * 初始化用户自定义词典
	 * C:\\Users\\Shu\\Desktop\\eclipse-jee-mars-1-win32-x86_64\\eclipse
	 * \\寻访记录\\User1.txt
	 */
	public static int init(String fileName) {
		InputStream in = null;
		String[] str = null;
		if (fileName != null && !fileName.equals("")) {
			try {
				in = Constants.class.getResourceAsStream(fileName);
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			return 1;
		}
		try {
			String tempStr = "";
			BufferedReader read = new BufferedReader(new InputStreamReader(in,"utf-8"));
			while ((tempStr = read.readLine()) != null) {
				str = tempStr.split(",|/");
				m_UserDic.put(str[0], Integer.parseInt(str[1]));
			}
			str = null;
			tempStr = null;
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (in != null) {
					in.close();
					in = null;
				}
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
		return 0;
	}

	/**
	 * 获取单元格内容
	 * 
	 * @param cell
	 *            单元格对象
	 * @return 转化为字符串的单元格内容
	 */
	private static String getCellContent(Cell cell) {
		return FORMATTER.formatCellValue(cell);
	}

	/* 判断是否为合并单元格 */
	public static boolean isMergedRegion(Sheet sheet, Cell cell) {
		// 得到一个sheet中有多少个合并单元格
		int sheetmergerCount = sheet.getNumMergedRegions();
		for (int i = 0; i < sheetmergerCount; i++) {
			// 得出具体的合并单元格
			CellRangeAddress ca = sheet.getMergedRegion(i);
			// 得到合并单元格的起始行, 结束行, 起始列, 结束列
			int firstC = ca.getFirstColumn();
			int lastC = ca.getLastColumn();
			int firstR = ca.getFirstRow();
			int lastR = ca.getLastRow();
			// 判断该单元格是否在合并单元格范围之内, 如果是, 则返回 true
			if (cell.getColumnIndex() <= lastC && cell.getColumnIndex() >= firstC) {
				if (cell.getRowIndex() <= lastR && cell.getRowIndex() >= firstR) {
					return true;
				}
			}
		}
		return false;
	}

	/* 识别合并单元格的内容 */
	public static String getMergedRegionValue(Sheet sheet, Cell cell) {
		// 获得一个 sheet 中合并单元格的数量
		int sheetmergerCount = sheet.getNumMergedRegions();
		// 便利合并单元格
		for (int i = 0; i < sheetmergerCount; i++) {
			// 获得合并单元格
			CellRangeAddress ca = sheet.getMergedRegion(i);
			// 获得合并单元格的起始行, 结束行, 起始列, 结束列
			int firstC = ca.getFirstColumn();
			int firstR = ca.getFirstRow();
			if (cell.getColumnIndex() == firstC && cell.getRowIndex() == firstR) {
				return getCellContent(cell);
			}
		}
		return "";
	}

	/**
	 * 读取Excel文件内容（单个sheet）
	 * 
	 */
	private static Map<String, Object> getExcelValue(String filePath, int sheetIndex, String userFileName,
			Workbook book) {
		Map<String, Object> map = new HashMap<String, Object>();
		int currentid = 0;
		String value = "";
		try {
			Sheet sheet = book.getSheetAt(sheetIndex);
			// 获取到Excel文件中的所有行数
			int rows = sheet.getLastRowNum();
			// 遍历行
			String[] strArray = new String[2000];
			DataHead dataHead = new DataHead();
			DataStructure[] dataStructureArray = new DataStructure[1000];
			int numberofdatastructure = 1;
			DataStructure dataStructure = null;
			for (int i = 0; i <= rows; i++) {
				dataStructure = new DataStructure();
				int compactcolomn = 0;
				// 读取左上端单元格
				Row row = sheet.getRow(i);
				// 行不为空
				if (row != null) {
					// 获取到Excel文件中的所有的列
					int cells = row.getPhysicalNumberOfCells();
					int output = 0;
					int output1 = 0;
					int output2 = 0;
					int output3 = 0;
					int output4 = 0;
					int output5 = 0;
					// 遍历列
					for (int j = 0; j < cells; j++) {
						// 获取到列的值
						Cell cell = row.getCell(j);
						if (cell != null) {
							if (isMergedRegion(sheet, cell)) { // 是合并单元格
								compactcolomn = 1;
								value = getMergedRegionValue(sheet, cell);
								if (value == "") {
									compactcolomn = 0;
								}
								// 解析合并单元格
								String text = value.replaceAll(" ", "");// 去除文本中的空格
								text = text.replaceAll("\\.", "/");
								text = text.replaceAll("\\-", "/");
								StringReader sr = new StringReader(text);// 读取文本
								IKSegmenter ik = new IKSegmenter(sr, true);// 分词
								Lexeme lex = null;
								dataHead.pageNum = sheetIndex;
								while ((lex = ik.next()) != null) {
									String tmptxt = "";
									String tmptxt1 = "";
									if (m_UserDic.containsKey(lex.getLexemeText())) {
										int type = m_UserDic.get(lex.getLexemeText());
										switch (type) {
										case 100:
											tmptxt = "Client";
											while ((lex = ik.next()) != null) {
												tmptxt1 = tmptxt1 + lex.getLexemeText();
											}
											if (output == 0) {
												dataHead.client += tmptxt1;
											}
											output = 1;
											break;
										case 101:
											tmptxt = "Title";
											while ((lex = ik.next()) != null) {
												tmptxt1 = tmptxt1 + lex.getLexemeText() + " ";
											}
											if (output1 == 0) {
												dataHead.title += tmptxt1;
											}
											output1 = 1;
											break;
										case 102:
											tmptxt = "Date";
											int tmpflag = 0;
											while ((lex = ik.next()) != null) {
												String tmpstr = lex.getLexemeText();
												for (int index = 0, len = tmpstr.length(); index < len; index++) {
													tmptxt1 = tmptxt1 + tmpstr.charAt(index);
													if (tmpstr.length() > 2) {
														tmpflag = 1;
													}
												}
												if (tmpflag == 0) {
													tmptxt1 = tmptxt1 + "/";
												}
											}
											if (output2 == 0) {
												dataHead.updatetime += tmptxt1;
											}
											output2 = 1;
											break;
										case 103:
											tmptxt = "Researcher";
											while ((lex = ik.next()) != null) {
												tmptxt1 = tmptxt1 + lex.getLexemeText() + " ";
											}
											if (output3 == 0) {
												dataHead.researcher += tmptxt1;
											}
											output3 = 1;
											break;
										case 105:
											tmptxt = "Consultant";
											while ((lex = ik.next()) != null) {
												tmptxt1 = tmptxt1 + lex.getLexemeText() + " ";
											}
											if (output5 == 0) {
												dataHead.consultant += tmptxt1;
											}
											output5 = 1;
											break;
										default:
											break;
										}// switch
									} // if
								} // while
							} else { // 非合并单元格
								// 解析非合并单元格
								String str = getCellContent(cell).substring(0).trim();
								if (str != "") {
									if (m_UserDic.containsKey(str)) {// 识别到表头
										int type = m_UserDic.get(str);
										switch (type) {
										case 100:
											strArray[j] = "所在公司";
											break;
										case 101:
											strArray[j] = "职位名称";
											break;
										case 102:
											strArray[j] = "更新日期";
											break;
										case 201:
											strArray[j] = "候选人姓名";
											break;
										case 202:
											strArray[j] = "性别";
											break;
										case 203:
											strArray[j] = "出生日期";
											break;
										case 204:
											strArray[j] = "婚姻状况";
											break;
										case 205:
											strArray[j] = "有无小孩";
											break;
										case 206:
											strArray[j] = "专业";
											break;
										case 207:
											strArray[j] = "学历";
											break;
										case 208:
											strArray[j] = "毕业院校";
											break;
										case 209:
											strArray[j] = "级别";
											break;
										case 210:
											strArray[j] = "产品领域";
											break;
										case 211:
											strArray[j] = "工作地点";
											break;
										case 212:
											strArray[j] = "工作年限";
											break;
										case 213:
											strArray[j] = "联系方式";
											break;
										case 214:
											strArray[j] = "联系记录";
											break;
										case 215:
											strArray[j] = "备注";
											break;
										case 216:
											strArray[j] = "其它";
											break;
										case 217:
											strArray[j] = "部门";
											break;
										case 218:
											strArray[j] = "目前薪水";
											break;
										case 219:
											strArray[j] = "个人主页";
											break;
										case 220:
											strArray[j] = "简历";
											break;
										case 221:
											strArray[j] = "职业路径";
											break;
										case 222:
											strArray[j] = "期望";
											break;
										case 223:
											strArray[j] = "英文名";
											break;
										case 224:
											strArray[j] = "产品";
											break;
										default:
											break;
										}// switch
									} // if
									else // 识别到的内容不是表头内容
									{
										str = str.replaceAll(" ", "");
										compactcolomn = 2;
										if (strArray[j] != null) {// 没有合并的单元格
																	// 且识别到表头
											if (strArray[j] == "联系方式") {
												// 区分联系方式
												Pattern landline = Pattern.compile(
														"\\d{3,5}-\\d{7,8}|\\d{3,5}-\\d{7,8}-\\d{1,5}|\\d{3,5}-\\d{7,8}\\/\\d{1,8}|\\(\\d{3,5}\\)\\d{7,8}|\\(\\d{3,5}\\)\\d{7,8}-\\d{1,5}|\\(\\d{3,5}\\)\\d{7,8}\\/\\d{1,8}");
												Matcher matcher = landline.matcher(str);
												while (matcher.find()) {
													dataStructure.phone += matcher.group() + " ";
												}
												Pattern mobile = Pattern.compile("13\\d{9}|15\\d{9}|18\\d{9}");
												Matcher matcher1 = mobile.matcher(str);
												while (matcher1.find()) {
													dataStructure.setMobile(
															dataStructure.getMobile() + matcher1.group() + " ");
												}
												Pattern emailaddress = Pattern.compile(
														"([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?)");
												Matcher matcher2 = emailaddress.matcher(str);
												while (matcher2.find()) {
													dataStructure.email += matcher2.group() + " ";
												}
											} else {
												if (strArray[j] != null) {
													if (strArray[j] == "所在公司") {
														dataStructure.company += str;
													} else if (strArray[j] == "职位名称") {
														dataStructure.title += str;
													} else if (strArray[j] == "更新日期") {
														dataStructure.updatetime += str;
													} else if (strArray[j] == "候选人姓名") {
														dataStructure.candidateName += str;
													} else if (strArray[j] == "性别") {
														dataStructure.gender += str;
													} else if (strArray[j] == "出生日期") {
														dataStructure.birthday += str;
													} else if (strArray[j] == "婚姻状况") {
														dataStructure.marriage += str;
													} else if (strArray[j] == "有无小孩") {
														dataStructure.withChild += str;
													} else if (strArray[j] == "专业") {
														dataStructure.major += str;
													} else if (strArray[j] == "学历") {
														dataStructure.degree += str;
													} else if (strArray[j] == "毕业院校") {
														dataStructure.school += str;
													} else if (strArray[j] == "级别") {
														dataStructure.jibie += str;
													} else if (strArray[j] == "产品领域") {
														dataStructure.productField += str;
													} else if (strArray[j] == "工作地点") {
														dataStructure.workingPlace += str;
													} else if (strArray[j] == "工作年限") {
														dataStructure.lengthOfService += str;
													} else if (strArray[j] == "联系记录") {
														dataStructure.connectionRecord += str;
													} else if (strArray[j] == "备注") {
														dataStructure.comment += str;
													} else if (strArray[j] == "部门") {
														// dataStructure.bumen
														// +=
														// getCellContent(cell);
													} else if (strArray[j] == "目前薪水") {
														// dataStructure.muqianxinshui
														// +=
														// getCellContent(cell);
													} else if (strArray[j] == "个人主页") {
														// dataStructure.gerenzhuye
														// +=
														// getCellContent(cell);
													} else if (strArray[j] == "简历") {
														// dataStructure.jianli
														// +=
														// getCellContent(cell);
													} else if (strArray[j] == "职业路径") {
														// dataStructure.zhiyelujing
														// +=
														// getCellContent(cell);
													} else if (strArray[j] == "期望") {
														// dataStructure.qiwang
														// +=
														// getCellContent(cell);
													} else if (strArray[j] == "英文名") {
														// dataStructure.yingwenming
														// +=
														// getCellContent(cell);
													} else if (strArray[j] == "产品") {
														// dataStructure.chanpin
														// +=
														// getCellContent(cell);
													} else if (strArray[j] == "其它") {
														dataStructure.other += str;
													}
												}
											}
										} // if(strArray[j] != null)
										else // 没有合并的单元格 且没有识别到表头
										{
											dataHead.filename = userFileName;
											dataHead.pageNum = sheetIndex;
											compactcolomn = 1;
											String tmpString = getCellContent(cell);
											String text = tmpString.replaceAll(" ", "");// 去除文本中的空格
											text = text.replaceAll("\\.", "/");// 将非标准日期分隔符统一为/
											text = text.replaceAll("\\-", "/");
											StringReader sr = new StringReader(text);// 读取文本
											IKSegmenter ik = new IKSegmenter(sr, true);// 分词
											Lexeme lex = null;
											// int size = text.length();
											while ((lex = ik.next()) != null) {
												String tmptxt = "";
												String tmptxt1 = "";
												if (m_UserDic.containsKey(lex.getLexemeText())) {
													int type = m_UserDic.get(lex.getLexemeText());
													switch (type) {
													case 100:
														tmptxt = "Client";
														while ((lex = ik.next()) != null) {
															tmptxt1 = tmptxt1 + lex.getLexemeText();
														}
														if (output == 0) {
															dataHead.client += tmptxt1;
														}
														output = 1;
														break;
													case 101:
														tmptxt = "Title";
														while ((lex = ik.next()) != null) {
															tmptxt1 = tmptxt1 + lex.getLexemeText() + " ";
														}
														if (output1 == 0) {
															dataHead.title += tmptxt1;
														}
														output1 = 1;
														break;
													case 102:
														tmptxt = "Date";
														int tmpflag = 0;
														while ((lex = ik.next()) != null) {
															String tmpstr = lex.getLexemeText();
															for (int index = 0, len = tmpstr
																	.length(); index < len; index++) {
																tmptxt1 = tmptxt1 + tmpstr.charAt(index);
																if (tmpstr.length() > 2) {
																	tmpflag = 1;
																}
															}
															if (tmpflag == 0) {
																tmptxt1 = tmptxt1 + "/";
															}
														}
														if (output2 == 0) {
															dataHead.updatetime += tmptxt1;
														}
														output2 = 1;
														break;
													case 103:
														tmptxt = "Researcher";
														while ((lex = ik.next()) != null) {
															tmptxt1 = tmptxt1 + lex.getLexemeText() + " ";
														}
														if (output3 == 0) {
															dataHead.researcher += tmptxt1;
														}
														output3 = 1;
														break;
													case 105:
														tmptxt = "Consultant";
														while ((lex = ik.next()) != null) {
															tmptxt1 = tmptxt1 + lex.getLexemeText() + " ";
														}
														if (output5 == 0) {
															dataHead.consultant += tmptxt1;
														}
														output5 = 1;
														break;
													default:
														// Field[] fields =
														// dataStructure.getClass().getDeclaredFields();
														// String oldstring =
														// (String)
														// fields[j].get(dataStructure);
														// oldstring += text;
														// fields[j].set(dataStructure,oldstring);
														// return
														break;
													}// switch
												} // if
											} // while
										}
									}
								} // if str == null
							}
						} // if cell == null
					} // 遍历列
				}
				if (dataStructure.dataStructureIsEmpty() == true) {
				} else {
					dataStructureArray[numberofdatastructure] = dataStructure;
					numberofdatastructure++;
				}
			} // 遍历行
			try {
				map.put("DataHead", dataHead);
			} catch (Exception e) {
			}
			// 寻访记录
			try {
				int datarow = 1;
				try {
					List<DataStructure> dList = new ArrayList<DataStructure>();
					while (dataStructureArray[datarow] != null && !dataStructureArray[datarow].dataStructureIsEmpty()) {
						if (dataStructureArray[datarow].dataStructureIsEmpty() == true) {
						} else {
							dList.add(dataStructureArray[datarow]);
						}
						datarow++;
					}
					map.put("DataStructure", dList);
				} catch (NullPointerException n) {
					n.printStackTrace();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		}
		return map;
	}

	/**
	 * 读取Excel文件内容（单个sheet）
	 * 
	 */
	private static void getExcelValueBehind(int id, String filePath, int sheetIndex, String userFileName, Workbook book,
			IDatabaseDao dao) {
		Map<String, Object> map = new HashMap<String, Object>();
		int currentid = 0;
		String value = "";
		try {
			Sheet sheet = book.getSheetAt(sheetIndex);
			// 获取到Excel文件中的所有行数
			int rows = sheet.getLastRowNum();
			// 遍历行
			String[] strArray = new String[2000];
			DataHead dataHead = new DataHead();
			DataStructure[] dataStructureArray = new DataStructure[1000];
			int numberofdatastructure = 1;
			DataStructure dataStructure = null;
			for (int i = 0; i <= rows; i++) {
				dataStructure = new DataStructure();
				int compactcolomn = 0;
				// 读取左上端单元格
				Row row = sheet.getRow(i);
				// 行不为空
				if (row != null) {
					// 获取到Excel文件中的所有的列
					int cells = row.getPhysicalNumberOfCells();
					int output = 0;
					int output1 = 0;
					int output2 = 0;
					int output3 = 0;
					int output4 = 0;
					int output5 = 0;
					// 遍历列
					for (int j = 0; j < cells; j++) {
						// 获取到列的值
						Cell cell = row.getCell(j);
						if (cell != null) {
							if (isMergedRegion(sheet, cell)) { // 是合并单元格
								compactcolomn = 1;
								value = getMergedRegionValue(sheet, cell);
								if (value == "") {
									compactcolomn = 0;
								}
								// 解析合并单元格
								String text = value.replaceAll(" ", "");// 去除文本中的空格
								text = text.replaceAll("\\.", "/");
								text = text.replaceAll("\\-", "/");
								StringReader sr = new StringReader(text);// 读取文本
								IKSegmenter ik = new IKSegmenter(sr, true);// 分词
								Lexeme lex = null;
								dataHead.pageNum = sheetIndex;
								while ((lex = ik.next()) != null) {
									String tmptxt = "";
									String tmptxt1 = "";
									if (m_UserDic.containsKey(lex.getLexemeText())) {
										int type = m_UserDic.get(lex.getLexemeText());
										switch (type) {
										case 100:
											tmptxt = "Client";
											while ((lex = ik.next()) != null) {
												tmptxt1 = tmptxt1 + lex.getLexemeText();
											}
											if (output == 0) {
												dataHead.client += tmptxt1;
											}
											output = 1;
											break;
										case 101:
											tmptxt = "Title";
											while ((lex = ik.next()) != null) {
												tmptxt1 = tmptxt1 + lex.getLexemeText() + " ";
											}
											if (output1 == 0) {
												dataHead.title += tmptxt1;
											}
											output1 = 1;
											break;
										case 102:
											tmptxt = "Date";
											int tmpflag = 0;
											while ((lex = ik.next()) != null) {
												String tmpstr = lex.getLexemeText();
												for (int index = 0, len = tmpstr.length(); index < len; index++) {
													tmptxt1 = tmptxt1 + tmpstr.charAt(index);
													if (tmpstr.length() > 2) {
														tmpflag = 1;
													}
												}
												if (tmpflag == 0) {
													tmptxt1 = tmptxt1 + "/";
												}
											}
											if (output2 == 0) {
												dataHead.updatetime += tmptxt1;
											}
											output2 = 1;
											break;
										case 103:
											tmptxt = "Researcher";
											while ((lex = ik.next()) != null) {
												tmptxt1 = tmptxt1 + lex.getLexemeText() + " ";
											}
											if (output3 == 0) {
												dataHead.researcher += tmptxt1;
											}
											output3 = 1;
											break;
										case 105:
											tmptxt = "Consultant";
											while ((lex = ik.next()) != null) {
												tmptxt1 = tmptxt1 + lex.getLexemeText() + " ";
											}
											if (output5 == 0) {
												dataHead.consultant += tmptxt1;
											}
											output5 = 1;
											break;
										default:
											break;
										}// switch
									} // if
								} // while
							} else { // 非合并单元格
								// 解析非合并单元格
								String str = getCellContent(cell).substring(0).trim();
								if (str != "") {
									if (m_UserDic.containsKey(str)) {// 识别到表头
										int type = m_UserDic.get(str);
										switch (type) {
										case 100:
											strArray[j] = "所在公司";
											break;
										case 101:
											strArray[j] = "职位名称";
											break;
										case 102:
											strArray[j] = "更新日期";
											break;
										case 201:
											strArray[j] = "候选人姓名";
											break;
										case 202:
											strArray[j] = "性别";
											break;
										case 203:
											strArray[j] = "出生日期";
											break;
										case 204:
											strArray[j] = "婚姻状况";
											break;
										case 205:
											strArray[j] = "有无小孩";
											break;
										case 206:
											strArray[j] = "专业";
											break;
										case 207:
											strArray[j] = "学历";
											break;
										case 208:
											strArray[j] = "毕业院校";
											break;
										case 209:
											strArray[j] = "级别";
											break;
										case 210:
											strArray[j] = "产品领域";
											break;
										case 211:
											strArray[j] = "工作地点";
											break;
										case 212:
											strArray[j] = "工作年限";
											break;
										case 213:
											strArray[j] = "联系方式";
											break;
										case 214:
											strArray[j] = "联系记录";
											break;
										case 215:
											strArray[j] = "备注";
											break;
										case 216:
											strArray[j] = "其它";
											break;
										case 217:
											strArray[j] = "部门";
											break;
										case 218:
											strArray[j] = "目前薪水";
											break;
										case 219:
											strArray[j] = "个人主页";
											break;
										case 220:
											strArray[j] = "简历";
											break;
										case 221:
											strArray[j] = "职业路径";
											break;
										case 222:
											strArray[j] = "期望";
											break;
										case 223:
											strArray[j] = "英文名";
											break;
										case 224:
											strArray[j] = "产品";
											break;
										default:
											break;
										}// switch
									} // if
									else // 识别到的内容不是表头内容
									{
										str = str.replaceAll(" ", "");
										compactcolomn = 2;
										if (strArray[j] != null) {// 没有合并的单元格
																	// 且识别到表头
											if (strArray[j] == "联系方式") {
												// 区分联系方式
												Pattern landline = Pattern.compile(
														"\\d{3,5}-\\d{7,8}|\\d{3,5}-\\d{7,8}-\\d{1,5}|\\d{3,5}-\\d{7,8}\\/\\d{1,8}|\\(\\d{3,5}\\)\\d{7,8}|\\(\\d{3,5}\\)\\d{7,8}-\\d{1,5}|\\(\\d{3,5}\\)\\d{7,8}\\/\\d{1,8}");
												Matcher matcher = landline.matcher(str);
												while (matcher.find()) {
													dataStructure.phone += matcher.group() + " ";
												}
												Pattern mobile = Pattern.compile("13\\d{9}|15\\d{9}|18\\d{9}");
												Matcher matcher1 = mobile.matcher(str);
												while (matcher1.find()) {
													dataStructure.setMobile(
															dataStructure.getMobile() + matcher1.group() + " ");
												}
												Pattern emailaddress = Pattern.compile(
														"([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?)");
												Matcher matcher2 = emailaddress.matcher(str);
												while (matcher2.find()) {
													dataStructure.email += matcher2.group() + " ";
												}
											} else {
												if (strArray[j] != null) {
													if (strArray[j] == "所在公司") {
														dataStructure.company += str;
													} else if (strArray[j] == "职位名称") {
														dataStructure.title += str;
													} else if (strArray[j] == "更新日期") {
														dataStructure.updatetime += str;
													} else if (strArray[j] == "候选人姓名") {
														dataStructure.candidateName += str;
													} else if (strArray[j] == "性别") {
														dataStructure.gender += str;
													} else if (strArray[j] == "出生日期") {
														dataStructure.birthday += str;
													} else if (strArray[j] == "婚姻状况") {
														dataStructure.marriage += str;
													} else if (strArray[j] == "有无小孩") {
														dataStructure.withChild += str;
													} else if (strArray[j] == "专业") {
														dataStructure.major += str;
													} else if (strArray[j] == "学历") {
														dataStructure.degree += str;
													} else if (strArray[j] == "毕业院校") {
														dataStructure.school += str;
													} else if (strArray[j] == "级别") {
														dataStructure.jibie += str;
													} else if (strArray[j] == "产品领域") {
														dataStructure.productField += str;
													} else if (strArray[j] == "工作地点") {
														dataStructure.workingPlace += str;
													} else if (strArray[j] == "工作年限") {
														dataStructure.lengthOfService += str;
													} else if (strArray[j] == "联系记录") {
														dataStructure.connectionRecord += str;
													} else if (strArray[j] == "备注") {
														dataStructure.comment += str;
													} else if (strArray[j] == "部门") {
														// dataStructure.bumen
														// +=
														// getCellContent(cell);
													} else if (strArray[j] == "目前薪水") {
														// dataStructure.muqianxinshui
														// +=
														// getCellContent(cell);
													} else if (strArray[j] == "个人主页") {
														// dataStructure.gerenzhuye
														// +=
														// getCellContent(cell);
													} else if (strArray[j] == "简历") {
														// dataStructure.jianli
														// +=
														// getCellContent(cell);
													} else if (strArray[j] == "职业路径") {
														// dataStructure.zhiyelujing
														// +=
														// getCellContent(cell);
													} else if (strArray[j] == "期望") {
														// dataStructure.qiwang
														// +=
														// getCellContent(cell);
													} else if (strArray[j] == "英文名") {
														// dataStructure.yingwenming
														// +=
														// getCellContent(cell);
													} else if (strArray[j] == "产品") {
														// dataStructure.chanpin
														// +=
														// getCellContent(cell);
													} else if (strArray[j] == "其它") {
														dataStructure.other += str;
													}
												}
											}
										} // if(strArray[j] != null)
										else // 没有合并的单元格 且没有识别到表头
										{
											dataHead.filename = userFileName;
											dataHead.pageNum = sheetIndex;
											compactcolomn = 1;
											String tmpString = getCellContent(cell);
											String text = tmpString.replaceAll(" ", "");// 去除文本中的空格
											text = text.replaceAll("\\.", "/");// 将非标准日期分隔符统一为/
											text = text.replaceAll("\\-", "/");
											StringReader sr = new StringReader(text);// 读取文本
											IKSegmenter ik = new IKSegmenter(sr, true);// 分词
											Lexeme lex = null;
											// int size = text.length();
											while ((lex = ik.next()) != null) {
												String tmptxt = "";
												String tmptxt1 = "";
												if (m_UserDic.containsKey(lex.getLexemeText())) {
													int type = m_UserDic.get(lex.getLexemeText());
													switch (type) {
													case 100:
														tmptxt = "Client";
														while ((lex = ik.next()) != null) {
															tmptxt1 = tmptxt1 + lex.getLexemeText();
														}
														if (output == 0) {
															dataHead.client += tmptxt1;
														}
														output = 1;
														break;
													case 101:
														tmptxt = "Title";
														while ((lex = ik.next()) != null) {
															tmptxt1 = tmptxt1 + lex.getLexemeText() + " ";
														}
														if (output1 == 0) {
															dataHead.title += tmptxt1;
														}
														output1 = 1;
														break;
													case 102:
														tmptxt = "Date";
														int tmpflag = 0;
														while ((lex = ik.next()) != null) {
															String tmpstr = lex.getLexemeText();
															for (int index = 0, len = tmpstr
																	.length(); index < len; index++) {
																tmptxt1 = tmptxt1 + tmpstr.charAt(index);
																if (tmpstr.length() > 2) {
																	tmpflag = 1;
																}
															}
															if (tmpflag == 0) {
																tmptxt1 = tmptxt1 + "/";
															}
														}
														if (output2 == 0) {
															dataHead.updatetime += tmptxt1;
														}
														output2 = 1;
														break;
													case 103:
														tmptxt = "Researcher";
														while ((lex = ik.next()) != null) {
															tmptxt1 = tmptxt1 + lex.getLexemeText() + " ";
														}
														if (output3 == 0) {
															dataHead.researcher += tmptxt1;
														}
														output3 = 1;
														break;
													case 105:
														tmptxt = "Consultant";
														while ((lex = ik.next()) != null) {
															tmptxt1 = tmptxt1 + lex.getLexemeText() + " ";
														}
														if (output5 == 0) {
															dataHead.consultant += tmptxt1;
														}
														output5 = 1;
														break;
													default:
														// Field[] fields =
														// dataStructure.getClass().getDeclaredFields();
														// String oldstring =
														// (String)
														// fields[j].get(dataStructure);
														// oldstring += text;
														// fields[j].set(dataStructure,oldstring);
														// return
														break;
													}// switch
												} // if
											} // while
										}
									}
								} // if str == null
							}
						} // if cell == null
					} // 遍历列
				}
				if (dataStructure.dataStructureIsEmpty() == true) {
				} else {
					dataStructureArray[numberofdatastructure] = dataStructure;
					numberofdatastructure++;
				}
			} // 遍历行
			try {
				dataHead.setOwnerId(id);
				System.out.println("dataHead recordId = " + dataHead.getOwnerId());
				map.put("DataHead", dataHead);
				dao.insertDataHead(dataHead);
			} catch (Exception e) {
			}
			// 寻访记录
			try {
				int datarow = 1;
				try {
					List<Map> dataheadid = dao.queryDataHead(dataHead);
					// System.out.println("list dataheadid = " + dataheadid);
					List<DataStructure> dList = new ArrayList<DataStructure>();
					while (dataStructureArray[datarow] != null && !dataStructureArray[datarow].dataStructureIsEmpty()) {
						if (dataStructureArray[datarow].dataStructureIsEmpty() == true) {
						} else {
							Iterator<Map> iter1 = dataheadid.iterator();
							while (iter1.hasNext()) {
								Map u1 = iter1.next();
								dataStructureArray[datarow].id = Integer.parseInt(u1.get("id").toString());
								// System.out.println("datastructure fatherId =
								// " + dataStructure.getId());
							}

							// System.out.println("datastructure fatherId = " +
							// dataStructure.getId());
							dList.add(dataStructureArray[datarow]);
							dao.insertDataStructure(dataStructureArray[datarow]);
						}
						datarow++;
					}
					map.put("DataStructure", dList);
				} catch (NullPointerException n) {
					n.printStackTrace();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		}
		// return map;
	}

	public static String parse(String filePathname) {
		int sheetIndex = 0;// 需要获取sheet数量，并分别处理
		// 创建对Excel工作簿文件
		Workbook book = null;
		if (filePathname.endsWith("xls")) {
			try {
				book = new HSSFWorkbook(new FileInputStream(filePathname));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else {
			try {
				book = new XSSFWorkbook(new FileInputStream(filePathname));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		String userFileName = "";
		Pattern fileName = Pattern.compile("([0-9A-Za-z\\-_\\.]+).xls|([0-9A-Za-z\\-_\\.]+).xlsx");
		Matcher matcher = fileName.matcher(filePathname);
		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
		while (matcher.find()) {
			userFileName += matcher.group();
			break;
		}
		if (book != null) {
			int numberOfSheet = book.getNumberOfSheets();
			for (sheetIndex = 0; sheetIndex < numberOfSheet; sheetIndex++) {
				Map<String, Object> map = getExcelValue(filePathname, sheetIndex, userFileName, book);
				result.add(map);
			}
		}
		return JSONUtil.ArrayToJSON(result);
	}

	public static void parseBehind(int id, String filePathname, IDatabaseDao dao) {
		System.out.println("=======parseBehind========");
		int sheetIndex = 0;// 需要获取sheet数量，并分别处理
		// 创建对Excel工作簿文件
		Workbook book = null;
		if (filePathname.endsWith("xls")) {
			try {
				book = new HSSFWorkbook(new FileInputStream(filePathname));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else {
			try {
				book = new XSSFWorkbook(new FileInputStream(filePathname));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		String userFileName = "";
		Pattern fileName = Pattern.compile("([0-9A-Za-z\\-_\\.]+).xls|([0-9A-Za-z\\-_\\.]+).xlsx");
		Matcher matcher = fileName.matcher(filePathname);
		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
		while (matcher.find()) {
			userFileName += matcher.group();
			break;
		}
		if (book != null) {
			dao.updateCVFileFlag(id);
			int numberOfSheet = book.getNumberOfSheets();
			for (sheetIndex = 0; sheetIndex < numberOfSheet; sheetIndex++) {
				getExcelValueBehind(id, filePathname, sheetIndex, userFileName, book, dao);
				// result.add(map);
			}
			// dao.updateCVFileFlag(id);
		}
	}
}
